package cn.xu.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

public class ProvinceServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		
		SAXReader saxReader = new SAXReader();
		InputStream input = this.getClass().getResourceAsStream("/china.xml");
		
		try {
			Document document = saxReader.read(input);
			List<Attribute> arrList = document.selectNodes("//province/@name");
			
			StringBuilder str = new StringBuilder();
			for(int i=0;i<arrList.size();i++){
				str.append(arrList.get(i).getValue());
				if(i< arrList.size()-1){
					str.append(",");	
				}
			}
			
			response.getWriter().print(str.toString());
			
		} catch (Exception e) {
			throw new RuntimeException();
		}
		
		
	}

}
